package com.ellricky.market.ui

import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.http.Api
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.http.callback.EListCallback
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.GlideUtil
import com.ellricky.market.MarketRankAdapter
import com.ellricky.market.MarketRankEntity
import com.ellricky.market.R
import de.hdodenhof.circleimageview.CircleImageView
import kotlinx.android.synthetic.main.market_activity_my_rank.*
import kotlinx.android.synthetic.main.market_adapter_rank_header.*

class MyRankActivity : BaseToolBarActivity<Void, BasePresenter<Void>>() {

    private lateinit var mAdapter: MarketRankAdapter
    private lateinit var dataList: List<MarketRankEntity>

    override fun getContentLayout(): Int {
        return R.layout.market_activity_my_rank
    }

    override fun initUIView() {
        super.initUIView()
        mTvTitle.text = getString(R.string.market_rank_list)

        rankRv.layoutManager = LinearLayoutManager(this)
        mAdapter = MarketRankAdapter(this)
        rankRv.adapter = mAdapter
        rankRefresh.setOnRefreshListener {
            loadData()
        }

        loadData()
    }


    private fun loadData() {
        val url = EAppUtil.getApiHost() + Api.API_MARKET_RANK_LIST
        val paramMap = mutableMapOf<String, Any>()
        // 角色类型
        paramMap["stype"] = LocalCacheUtil.getCardType()
        // 查询的种类
        paramMap["flag"] = "1"
        // 是否为年 1=否 2= 是
        paramMap["isYear"] = "1"
        HttpUtils.getInstance().post(url, paramMap, object : EListCallback<MarketRankEntity>(this) {

            override fun onSuccess(data: MutableList<MarketRankEntity>?, msg: String?) {
                dataList = data ?: mutableListOf()
                val dataSize = dataList.size
                if (dataSize > 0) {
                    updateItemUI(dataList[0], ivFirst, nameFirst, numFirst)
                }
                if (dataSize > 1) {
                    updateItemUI(dataList[1], ivSecond, nameSecond, numSecond)
                }

                if (dataSize > 2) {
                    updateItemUI(dataList[2], ivThird, nameThird, numThird)
                }

                if (dataSize > 3) {
                    val adapterDataList = dataList.subList(3, dataList.size)
                    mAdapter.refresh(adapterDataList)
                }
            }


            override fun onAfter(id: Int) {
                super.onAfter(id)
                rankRefresh.finishRefresh()
            }
        })

    }


    private fun updateItemUI(entity: MarketRankEntity?,
                             iv: CircleImageView, tvName: TextView, tvScore: TextView) {

        entity?.let {
            GlideUtil.into(this, it.headPic, iv)
            tvName.text = it.name
            tvScore.text = it.totalScore

        }


    }


}
